package com.jeeplus.modules.work.utils;

import com.sun.jna.Native;

import java.util.HashMap;
import java.util.Map;

public class IDCARDUtils {
    public static void main(String[] args) {
        getCardValue();
    }
    public static Map<String,String> getCardValue() {
        Map<String,String> map = new HashMap <String,String>();
        int[] text_len = new int[1];
        byte[] text = new byte[256];
        int[] photo_len = new int[1];
        byte[] photo = new byte[1024];
        int[] fingerprint_len = new int[1];
        byte[] fingerprint = new byte[1024];
        int[] extra_len = new int[1];
        byte[] extra = new byte[70];
        int type = 0;
        int icdev = -1;
        int st = -1;
        Reader.Dcrf32 dll_instance = (Reader.Dcrf32) Native.loadLibrary("C:\\Users\\18482\\Desktop\\gxsq部署\\windows\\64bit\\dcrf32.dll", Reader.Dcrf32.class);
        st = dll_instance.dc_init((short) 100, 115200);
        String result = "";
        if (st < 0) {
            if(icdev > 0)
            {
                st = dll_instance.dc_exit(icdev);
                if(st !=0 )
                {
                    return null;
                }
                else
                {
                    icdev = -1;
                }
            }
            return null;
        }

        dll_instance.dc_beep(st, (short)10);
        icdev = st;
        st = dll_instance.dc_SamAReadCardInfo(icdev, 3, text_len, text, photo_len, photo, fingerprint_len, fingerprint, extra_len, extra);
        if(st == 0){
            System.out.println("身份证读取成功。");
        }
        if ((text[0] >= 'A') && (text[0] <= 'Z') && (text[1] == 0)) {
            type = 1;
        }
        if (type == 0) {
            //卡类型：中国人居民身份证}
            byte[] name = new byte[64];
            byte[] sex = new byte[8];
            byte[] nation = new byte[12];
            byte[] birth_day = new byte[36];
            byte[] address = new byte[144];
            byte[] id_number = new byte[76];
            byte[] department = new byte[64];
            byte[] expire_start_day = new byte[36];
            byte[] expire_end_day = new byte[36];
            byte[] reserved = new byte[76];
            st = dll_instance.dc_ParseTextInfo(icdev, 0, text_len[0], text, name, sex, nation, birth_day, address, id_number, department, expire_start_day, expire_end_day, reserved);
            if(st >= 0){
                map.put("name",Reader.gbk_bytes_to_string(name));
                map.put("idcard",Reader.gbk_bytes_to_string(id_number));
                System.out.println(Reader.gbk_bytes_to_string(name));
                System.out.println(Reader.gbk_bytes_to_string(id_number));
            }
            if (st < 0) {
                if(icdev > 0)
                {
                    st = dll_instance.dc_exit(icdev);
                    if(st !=0 )
                    {
                        return null;
                    }
                    else
                    {
                        icdev = -1;
                    }
                }
                return null;
            }
        }else{
            //卡类型：外国人永久居留证
            byte[] english_name = new byte[244];
            byte[] sex = new byte[8];
            byte[] id_number = new byte[64];
            byte[] citizenship = new byte[16];
            byte[] chinese_name = new byte[64];
            byte[] expire_start_day = new byte[36];
            byte[] expire_end_day = new byte[36];
            byte[] birth_day = new byte[36];
            byte[] version_number = new byte[12];
            byte[] department_code = new byte[20];
            byte[] type_sign = new byte[8];
            byte[] reserved = new byte[16];
            st = dll_instance.dc_ParseTextInfoForForeigner(icdev, 0, text_len[0], text, english_name, sex, id_number, citizenship, chinese_name, expire_start_day, expire_end_day, birth_day, version_number, department_code, type_sign, reserved);
            if(st >= 0){
                map.put("name",Reader.gbk_bytes_to_string(english_name));
                map.put("idcard",Reader.gbk_bytes_to_string(id_number));
                System.out.println(Reader.gbk_bytes_to_string(english_name));
                System.out.println(Reader.gbk_bytes_to_string(id_number));
            }
            if (st < 0) {
                if(icdev > 0)
                {
                    st = dll_instance.dc_exit(icdev);
                    if(st !=0 )
                    {
                        return null;
                    }
                    else
                    {
                        icdev = -1;
                    }
                }
                return null;
            }
        }
        dll_instance = null;
        System.gc(); // 建议JVM进行垃圾回收
        return map;
    }
}
